/*
 * Copyright 2013 Wajdi Hh "wajdihh@gmail.com".
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hh.ui.dialog;

import java.util.Calendar;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;

import com.hh.droid.HhDroid;
import com.hh.droid.R;
import com.hh.listeners.OnDateSetListener;

/**
 * <br>
 * A dialog showing a date picker widget + number of week  with holo light style
 * 
 * @author WajdiHh : Last modification : 23/05/2013
 *
 */
public class UiDialogDatePicker extends Dialog implements
		android.view.View.OnClickListener {

	private String _mNewDate;
	private DatePicker _mDatePicker;
	private TextView _mTVTitle,_mTVWeek;
	private Button _mButOk, _mButCancel;
	private int _mDay, _mMonth, _mYear;
	private OnDateSetListener _mDateSetListner;
	private Context _mContext;

	/**<br>
	 * Dialog constructor
	 * 
	 * @author WajdiHh
	 * @param context  the context the view is running in
	 * @param dayOfMonth : initial launcher day value
	 * @param monthOfYear: initial launcher month value
	 * @param year: initial launcher year value
	 */
	
	public UiDialogDatePicker(Context context,int dayOfMonth ,int monthOfYear,int year) {
		super(context);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ui_dialog_datepicker);

		_mTVTitle = (TextView) findViewById(R.id.uiDateDialog_TvTitle);
		_mTVWeek = (TextView) findViewById(R.id.uiDateDialog_TvWeek);
		_mContext = context;

		_mButOk = (Button) findViewById(R.id.uiDateDialog_BtnPositiveButton);
		_mButOk.setBackgroundResource(R.drawable.selector_button);

		_mButOk.setText(context.getResources().getString(R.string.bt_alertbox_Validate));
		_mButOk.setOnClickListener(this);

		_mButCancel = (Button) findViewById(R.id.uiDateDialog_BtnNegativeButton);
		_mButCancel.setBackgroundResource(R.drawable.selector_button);

		_mButCancel.setText(context.getResources().getString(R.string.bt_alertbox_Cancel));
		_mButCancel.setOnClickListener(this);

		monthOfYear=monthOfYear-1;
		_mDay = dayOfMonth;
		_mMonth = monthOfYear;
		_mYear = year;

		_mDatePicker = (DatePicker) findViewById(R.id.uiDateDialog_DatePicker);

		_mDatePicker.init(year, monthOfYear, dayOfMonth,new OnDateChangedListener() {
					@Override
					public void onDateChanged(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
						_mDay = dayOfMonth;
						_mMonth = monthOfYear;
						_mYear = year;
						updateTitle(year, monthOfYear, dayOfMonth);
					}
				});

		this.setCanceledOnTouchOutside(true);
		updateTitle(year, monthOfYear, dayOfMonth);
	}

	/**
	 * <br>
	 * Register a callback to be invoked when date is selected from the date picker widget.
	 * 
	 * @author WajdiHh
	 * @param pListener: the listener to handle selection date
	 * @see OnDateSetListener
	 */
	public void setOnDateSetListener(OnDateSetListener pListener) {
		_mDateSetListner = pListener;
	}

	
	private void updateTitle(int year, int month, int day) {
		Calendar mCalendar = Calendar.getInstance();
		mCalendar.set(Calendar.YEAR, year);
		mCalendar.set(Calendar.MONTH, month);
		mCalendar.set(Calendar.DAY_OF_MONTH, day);
		int nWeek = mCalendar.get(Calendar.WEEK_OF_YEAR);

		_mNewDate = HhDroid.getInstance().mDateFormat.format(mCalendar.getTime());
		_mTVTitle.setText((_mNewDate));
		_mTVWeek.setText(( _mContext.getResources().getString(R.string.word_week)+ " : " + nWeek));
	}
	
	@Override
	public void onClick(View v) {
		int lId = v.getId();
		if (lId == R.id.uiDateDialog_BtnNegativeButton) {
			this.dismiss();
		} else if (lId == R.id.uiDateDialog_BtnPositiveButton) {
			if(_mDateSetListner!=null)
				_mDateSetListner.onDateSetClick(_mDay,(_mMonth+1),_mYear);
			
			this.dismiss();
		}
	};

}
